package com.nvshen.zhangmeiying;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nvshen.zhangmeiying.life.LifeActivity;
import com.nvshen.zhangmeiying.profile.ProfileActivity;
import com.umeng.analytics.MobclickAgent;


public class MainTabActivity extends TabActivity implements View.OnClickListener
{

    private static final String TAG_HOME = "资料";
    private static final String TAG_FEED = "生活";
    private static final String TAG_RECORD = "星路";
    private static final String TAG_MESSAGE = "粉丝";
    private static final String TAG_MINE = "送礼";

    TabHost tabHost;

    TextView tabText0, tabText1, tabText2, tabText3, tabText4;
    private int curTabIndex = 0;
//    private User curUid;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        Constants.SCREEN_WIDTH = metric.widthPixels;  // 屏幕宽度（像素）
        Constants.SCREEN_HEIGHT = metric.heightPixels;  // 屏幕高度（像素

        //init imageloader
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .discCacheFileCount(100)
                .build();
        ImageLoader.getInstance().init(config);

//        curUid = (User) getIntent().getSerializableExtra("user");
//        if (curUid == null)
//        {
//            Toast.makeText(this, "获取用户失败", Toast.LENGTH_SHORT).show();
//            finish();
//        }

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        initViews();
        initTab(savedInstanceState);
    }

    private void initTab(Bundle savedInstanceState)
    {
        tabHost = getTabHost();
        addTab(TAG_HOME, R.drawable.home_normal, ProfileActivity.class);
        addTab(TAG_FEED, R.drawable.home_normal, LifeActivity.class);
//        addTab(TAG_RECORD, R.drawable.home_normal, ProfileXingluActivity.class);
//        addTab(TAG_MESSAGE, R.drawable.home_normal, ProfileFenxiActivity.class);
//        addTab(TAG_MINE, R.drawable.home_normal, GiveGiftActivity.class);

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener()
        {

            @Override
            public void onTabChanged(String arg0)
            {
                updateTab();
            }
        });
        tabHost.setCurrentTab(curTabIndex);
        updateTab();
    }

    private void initViews()
    {
        tabText0 = (TextView) findViewById(R.id.text_tab_0);
        tabText1 = (TextView) findViewById(R.id.text_tab_1);
        tabText2 = (TextView) findViewById(R.id.text_tab_2);
        tabText3 = (TextView) findViewById(R.id.text_tab_3);
        tabText4 = (TextView) findViewById(R.id.text_tab_4);

        findViewById(R.id.layout_tab_0).setOnClickListener(this);
        findViewById(R.id.layout_tab_1).setOnClickListener(this);
        findViewById(R.id.layout_tab_2).setOnClickListener(this);
        findViewById(R.id.layout_tab_3).setOnClickListener(this);
        findViewById(R.id.layout_tab_4).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.layout_tab_0:
                curTabIndex = 0;
                tabHost.setCurrentTab(0);
                break;
            case R.id.layout_tab_1:
                curTabIndex = 1;
                tabHost.setCurrentTab(1);
                break;
            case R.id.layout_tab_2:
                curTabIndex = 2;
                tabHost.setCurrentTab(2);
                break;
            case R.id.layout_tab_3:
                curTabIndex = 3;
                tabHost.setCurrentTab(3);
                break;
            case R.id.layout_tab_4:
                curTabIndex = 4;
                tabHost.setCurrentTab(4);
                break;
        }
    }

    /**
     * 添加tab
     *
     * @param labelId
     * @param drawableId
     * @param c
     */
    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec(labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

    private void updateTab()
    {
        switch (curTabIndex)
        {
            case 0:
                tabText0.setTextColor(getResources().getColor(R.color.tab_selected));
                tabText1.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText2.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText3.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText4.setTextColor(getResources().getColor(R.color.tab_unselected));
                break;
            case 1:
                tabText0.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText1.setTextColor(getResources().getColor(R.color.tab_selected));
                tabText2.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText3.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText4.setTextColor(getResources().getColor(R.color.tab_unselected));
                break;
            case 2:
                tabText0.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText1.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText2.setTextColor(getResources().getColor(R.color.tab_selected));
                tabText3.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText4.setTextColor(getResources().getColor(R.color.tab_unselected));
                break;
            case 3:
                tabText0.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText1.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText2.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText3.setTextColor(getResources().getColor(R.color.tab_selected));
                tabText4.setTextColor(getResources().getColor(R.color.tab_unselected));
                break;
            case 4:
                tabText0.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText1.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText2.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText3.setTextColor(getResources().getColor(R.color.tab_unselected));
                tabText4.setTextColor(getResources().getColor(R.color.tab_selected));
                break;
        }
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}